package com.carlinfo.userscenter.stu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.userscenter.stu.dao.ILevelDao;
import com.carlinfo.userscenter.stu.dao.IStudentsDao;
import com.carlinfo.userscenter.stu.dao.impl.levelDaoImpl;
import com.carlinfo.userscenter.stu.dao.impl.studentsDaoImpl;
import com.carlinfo.userscenter.stu.pojo.Level;
import com.carlinfo.userscenter.stu.pojo.Students;
import com.carlinfo.userscenter.stu.service.IStudentService;

public class StudentServiceImpl implements IStudentService
{
	IStudentsDao studentDao = new studentsDaoImpl();
	ILevelDao levelDao = new levelDaoImpl();

	/*-------------------------学生操作开始----------------------------------*/
	@Override
	public JSONObject saveOneStudentsService(Students students)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.studentDao.saveOneDao(students);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", students.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public List<Students> selectListStudentsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		List<Students> list = this.studentDao.selectListDao(pageInfoUtil,condMap);
		for (Students students : list)
		{
			double sorce = students.getScore();
			List<Level> li = selectListLevelService(null, condMap);
			for (Level level : li)
			{
				if(sorce>=level.getStartMarks() && sorce<=level.getFinalMarks()) {
					students.setLevel(level);
				}
			}
		}
		return list;
	}

	@Override
	public Students selectOneStudentsService(Map<String, Object> condMap)
	{
		Students students = this.studentDao.selectOneDao(condMap);
		double sorce = students.getScore();
		List<Level> li = selectListLevelService(null, condMap);
		for (Level level : li)
		{
			if(sorce>=level.getStartMarks() && sorce<=level.getFinalMarks()) {
				students.setLevel(level);
			}
		}
		return students;
	}

	@Override
	public JSONObject updateOneStudentsService(Students students)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.studentDao.updateOneDao(students);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", students.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public JSONObject deleteOneStudentsService(Map<String, Object> condMap)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.studentDao.deleteOneDao(condMap);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	/*-------------------------学生操作结束----------------------------------*/
	/*-------------------------皇上操作开始----------------------------------*/


	@Override
	public JSONObject saveOneLevelService(Level level)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.levelDao.saveOneDao(level);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", level.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public List<Level> selectListLevelService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Level> list = this.levelDao.selectListDao(pageInfoUtil, condMap);
		return list;
	}

	@Override
	public Level selectOneLevelService(Map<String, Object> condMap)
	{
		Level level =this.levelDao.selectOneDao(condMap);
		return level;
	}

	@Override
	public JSONObject updateOneLevelService(Level level)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.levelDao.updateOneDao(level);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", level.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	
	@Override
	public JSONObject deleteOneLevelService(Map<String, Object> condMap)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.levelDao.deleteOneDao(condMap);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	/*-------------------------皇上操作结束----------------------------------*/

	@Override
	public int selectMaxIdService()
	{
		return this.studentDao.selectMaxId();
	}

	@Override
	public boolean askIsStudentService(Students students)
	{
		if(students==null) {
			return false;
		}
		int maxCount = 0;
		int askMaxCount = 0;
		boolean flag = true;
		boolean askFlag = true;
		Map<String, Object> condMap = new HashMap<String, Object>();
		List<Students> list = selectListStudentsService(null, condMap);
		for (Students student : list)
		{
			if (student.getTotalCount() > maxCount)
			{
				maxCount = student.getTotalCount();
			}
			if (askMaxCount == 0 && flag)
			{
				askMaxCount = student.getTotalCount();
				flag = false;
			}
			if (askMaxCount != student.getTotalCount())
			{
				askFlag = false;
			}
		}
		if (students.getTotalCount() < maxCount || maxCount == 0 || askFlag)
		{
			return true;
		}
		return false;
	}



}
